package cn.jiyun.ai.controller;

import cn.jiyun.ai.UsersService;
import cn.jiyun.ai.common.result.R;
import cn.jiyun.ai.pojo.Users;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("user")
@CrossOrigin
public class UsersController {
    private static final Logger log = LoggerFactory.getLogger(UsersController.class);
    @Autowired
    UsersService usersService;

    @RequestMapping("yanm")
    public R yanm(String mi){
        String regex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).+$";
        Pattern pattern1 = Pattern.compile(regex);
        Matcher matcher1 = pattern1.matcher(mi);
        if (!matcher1.matches()){
            return R.error().message("密码格式有误，请重新填写！");
        }
        return R.ok();
    }

    // 用户登录
    @RequestMapping("login")
    public R login(@RequestBody Users user){
        log.info("{}",user);
        return usersService.login(user);
    }

    // 处理忘记密码请求
    @RequestMapping("forget")
    public R forget(Users user) {
        return usersService.forget(user);
    }

    // 获取验证码（注册）
    @RequestMapping("yan")
    public R yan(Users user) throws Exception {
        return usersService.yan(user);
    }

    // 获取验证码（忘记）
    @RequestMapping("yan1")
    public R yan1(Users user) throws Exception {
        return usersService.yan1(user);
    }
}
